{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "_You are currently looking at **version 1.2** of this notebook. To download notebooks and datafiles, as well as get help on Jupyter notebooks in the Coursera platform, visit the [Jupyter Notebook FAQ](https://www.coursera.org/learn/python-data-analysis/resources/0dhYG) course resource._\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Assignment 2 - Pandas Introduction\n",
    "All questions are weighted the same in this assignment.\n",
    "## Part 1\n",
    "The following code loads the olympics dataset (olympics.csv), which was derrived from the Wikipedia entry on [All Time Olympic Games Medals](https://en.wikipedia.org/wiki/All-time_Olympic_Games_medal_table), and does some basic data cleaning. \n",
    "\n",
    "The columns are organized as # of Summer games, Summer medals, # of Winter games, Winter medals, total # number of games, total # of medals. Use this dataset to answer the questions below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "1",
     "locked": false,
     "solution": false
    },
    "umich_question": "prolog-000"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th># Summer</th>\n",
       "      <th>Gold</th>\n",
       "      <th>Silver</th>\n",
       "      <th>Bronze</th>\n",
       "      <th>Total</th>\n",
       "      <th># Winter</th>\n",
       "      <th>Gold.1</th>\n",
       "      <th>Silver.1</th>\n",
       "      <th>Bronze.1</th>\n",
       "      <th>Total.1</th>\n",
       "      <th># Games</th>\n",
       "      <th>Gold.2</th>\n",
       "      <th>Silver.2</th>\n",
       "      <th>Bronze.2</th>\n",
       "      <th>Combined total</th>\n",
       "      <th>ID</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Afghanistan</th>\n",
       "      <td>13</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>AFG</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Algeria</th>\n",
       "      <td>12</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>8</td>\n",
       "      <td>15</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>15</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>8</td>\n",
       "      <td>15</td>\n",
       "      <td>ALG</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Argentina</th>\n",
       "      <td>23</td>\n",
       "      <td>18</td>\n",
       "      <td>24</td>\n",
       "      <td>28</td>\n",
       "      <td>70</td>\n",
       "      <td>18</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>41</td>\n",
       "      <td>18</td>\n",
       "      <td>24</td>\n",
       "      <td>28</td>\n",
       "      <td>70</td>\n",
       "      <td>ARG</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Armenia</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "      <td>12</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "      <td>12</td>\n",
       "      <td>ARM</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Australasia</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>12</td>\n",
       "      <td>ANZ</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             # Summer  Gold  Silver  Bronze  Total  # Winter  Gold.1  \\\n",
       "Afghanistan        13     0       0       2      2         0       0   \n",
       "Algeria            12     5       2       8     15         3       0   \n",
       "Argentina          23    18      24      28     70        18       0   \n",
       "Armenia             5     1       2       9     12         6       0   \n",
       "Australasia         2     3       4       5     12         0       0   \n",
       "\n",
       "             Silver.1  Bronze.1  Total.1  # Games  Gold.2  Silver.2  Bronze.2  \\\n",
       "Afghanistan         0         0        0       13       0         0         2   \n",
       "Algeria             0         0        0       15       5         2         8   \n",
       "Argentina           0         0        0       41      18        24        28   \n",
       "Armenia             0         0        0       11       1         2         9   \n",
       "Australasia         0         0        0        2       3         4         5   \n",
       "\n",
       "             Combined total   ID  \n",
       "Afghanistan               2  AFG  \n",
       "Algeria                  15  ALG  \n",
       "Argentina                70  ARG  \n",
       "Armenia                  12  ARM  \n",
       "Australasia              12  ANZ  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv('olympics.csv', index_col=0, skiprows=1)\n",
    "\n",
    "for col in df.columns:\n",
    "    if col[:2]=='01':\n",
    "        df.rename(columns={col:'Gold'+col[4:]}, inplace=True)\n",
    "    if col[:2]=='02':\n",
    "        df.rename(columns={col:'Silver'+col[4:]}, inplace=True)\n",
    "    if col[:2]=='03':\n",
    "        df.rename(columns={col:'Bronze'+col[4:]}, inplace=True)\n",
    "    if col[:1]=='№':\n",
    "        df.rename(columns={col:'#'+col[1:]}, inplace=True)\n",
    "\n",
    "names_ids = df.index.str.split('\\s\\(') # split the index by '('\n",
    "\n",
    "df.index = names_ids.str[0] # the [0] element is the country name (new index) \n",
    "df['ID'] = names_ids.str[1].str[:3] # the [1] element is the abbreviation or ID (take first 3 characters from that)\n",
    "\n",
    "df = df.drop('Totals')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Question 0 (Example)\n",
    "\n",
    "What is the first country in df?\n",
    "\n",
    "*This function should return a Series.*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "umich_question": "000"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "# Summer           13\n",
       "Gold                0\n",
       "Silver              0\n",
       "Bronze              2\n",
       "Total               2\n",
       "# Winter            0\n",
       "Gold.1              0\n",
       "Silver.1            0\n",
       "Bronze.1            0\n",
       "Total.1             0\n",
       "# Games            13\n",
       "Gold.2              0\n",
       "Silver.2            0\n",
       "Bronze.2            2\n",
       "Combined total      2\n",
       "ID                AFG\n",
       "Name: Afghanistan, dtype: object"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# You should write your whole answer within the function provided. The autograder will call\n",
    "# this function and compare the return value against the correct solution value\n",
    "def answer_zero():\n",
    "    # This function returns the row for Afghanistan, which is a Series object. The assignment\n",
    "    # question description will tell you the general format the autograder is expecting\n",
    "    return df.iloc[0]\n",
    "\n",
    "# You can examine what your function returns by calling it in the cell. If you have questions\n",
    "# about the assignment formats, check out the discussion forums for any FAQs\n",
    "answer_zero() "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Question 1\n",
    "Which country has won the most gold medals in summer games?\n",
    "\n",
    "*This function should return a single string value.*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "locked": false,
     "solution": false
    },
    "umich_part_id": "001",
    "umich_partlist_id": "001"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'United States'"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def answer_one():\n",
    "    return df['Gold'].argmax()\n",
    "answer_one()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Question 2\n",
    "Which country had the biggest difference between their summer and winter gold medal counts?\n",
    "\n",
    "*This function should return a single string value.*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "umich_part_id": "002",
    "umich_partlist_id": "001"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'United States'"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def answer_two():\n",
    "    return (df['Gold'] - df['Gold.1']).argmax()\n",
    "answer_two()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Question 3\n",
    "Which country has the biggest difference between their summer gold medal counts and winter gold medal counts relative to their total gold medal count? \n",
    "\n",
    "$$\\frac{Summer~Gold - Winter~Gold}{Total~Gold}$$\n",
    "\n",
    "Only include countries that have won at least 1 gold in both summer and winter.\n",
    "\n",
    "*This function should return a single string value.*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "umich_part_id": "003",
    "umich_partlist_id": "001"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Bulgaria'"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def answer_three():\n",
    "    \n",
    "    return ((df['Gold'][(df['Gold']>0)] - df['Gold.1'][(df['Gold.1']>0)])/df['Gold.2']).argmax()\n",
    "\n",
    "answer_three()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Question 4\n",
    "Write a function that creates a Series called \"Points\" which is a weighted value where each gold medal (`Gold.2`) counts for 3 points, silver medals (`Silver.2`) for 2 points, and bronze medals (`Bronze.2`) for 1 point. The function should return only the column (a Series object) which you created, with the country names as indices.\n",
    "\n",
    "*This function should return a Series named `Points` of length 146*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "umich_part_id": "004",
    "umich_partlist_id": "001"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Afghanistan                            2\n",
       "Algeria                               27\n",
       "Argentina                            130\n",
       "Armenia                               16\n",
       "Australasia                           22\n",
       "Australia                            919\n",
       "Austria                              488\n",
       "Azerbaijan                            43\n",
       "Bahamas                               24\n",
       "Bahrain                                1\n",
       "Barbados                               1\n",
       "Belarus                              149\n",
       "Belgium                              273\n",
       "Bermuda                                1\n",
       "Bohemia                                5\n",
       "Botswana                               2\n",
       "Brazil                               184\n",
       "British West Indies                    2\n",
       "Bulgaria                             408\n",
       "Burundi                                3\n",
       "Cameroon                              12\n",
       "Canada                               794\n",
       "Chile                                 24\n",
       "China                               1101\n",
       "Colombia                              29\n",
       "Costa Rica                             7\n",
       "Ivory Coast                            2\n",
       "Croatia                               66\n",
       "Cuba                                 420\n",
       "Cyprus                                 2\n",
       "                                    ... \n",
       "Spain                                267\n",
       "Sri Lanka                              4\n",
       "Sudan                                  2\n",
       "Suriname                               4\n",
       "Sweden                              1163\n",
       "Switzerland                          582\n",
       "Syria                                  6\n",
       "Chinese Taipei                        32\n",
       "Tajikistan                             4\n",
       "Tanzania                               4\n",
       "Thailand                              44\n",
       "Togo                                   1\n",
       "Tonga                                  2\n",
       "Trinidad and Tobago                   27\n",
       "Tunisia                               19\n",
       "Turkey                               191\n",
       "Uganda                                14\n",
       "Ukraine                              216\n",
       "United Arab Emirates                   3\n",
       "United States                       5600\n",
       "Uruguay                               16\n",
       "Uzbekistan                            38\n",
       "Venezuela                             18\n",
       "Vietnam                                4\n",
       "Virgin Islands                         2\n",
       "Yugoslavia                           170\n",
       "Independent Olympic Participants       4\n",
       "Zambia                                 3\n",
       "Zimbabwe                              18\n",
       "Mixed team                            38\n",
       "dtype: int64"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def answer_four():\n",
    "    return df['Gold.2']*3 + df['Silver.2']*2 + df['Bronze']*1\n",
    "\n",
    "answer_four()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part 2\n",
    "For the next set of questions, we will be using census data from the [United States Census Bureau](http://www.census.gov). Counties are political and geographic subdivisions of states in the United States. This dataset contains population data for counties and states in the US from 2010 to 2015. [See this document](https://www2.census.gov/programs-surveys/popest/technical-documentation/file-layouts/2010-2015/co-est2015-alldata.pdf) for a description of the variable names.\n",
    "\n",
    "The census dataset (census.csv) should be loaded as census_df. Answer questions using this as appropriate.\n",
    "\n",
    "### Question 5\n",
    "Which state has the most counties in it? (hint: consider the sumlevel key carefully! You'll need this for future questions too...)\n",
    "\n",
    "*This function should return a single string value.*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 211,
   "metadata": {
    "umich_question": "prolog-005"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SUMLEV</th>\n",
       "      <th>REGION</th>\n",
       "      <th>DIVISION</th>\n",
       "      <th>STATE</th>\n",
       "      <th>COUNTY</th>\n",
       "      <th>STNAME</th>\n",
       "      <th>CTYNAME</th>\n",
       "      <th>CENSUS2010POP</th>\n",
       "      <th>ESTIMATESBASE2010</th>\n",
       "      <th>POPESTIMATE2010</th>\n",
       "      <th>...</th>\n",
       "      <th>RDOMESTICMIG2011</th>\n",
       "      <th>RDOMESTICMIG2012</th>\n",
       "      <th>RDOMESTICMIG2013</th>\n",
       "      <th>RDOMESTICMIG2014</th>\n",
       "      <th>RDOMESTICMIG2015</th>\n",
       "      <th>RNETMIG2011</th>\n",
       "      <th>RNETMIG2012</th>\n",
       "      <th>RNETMIG2013</th>\n",
       "      <th>RNETMIG2014</th>\n",
       "      <th>RNETMIG2015</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>40</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Alabama</td>\n",
       "      <td>Alabama</td>\n",
       "      <td>4779736</td>\n",
       "      <td>4780127</td>\n",
       "      <td>4785161</td>\n",
       "      <td>...</td>\n",
       "      <td>0.002295</td>\n",
       "      <td>-0.193196</td>\n",
       "      <td>0.381066</td>\n",
       "      <td>0.582002</td>\n",
       "      <td>-0.467369</td>\n",
       "      <td>1.030015</td>\n",
       "      <td>0.826644</td>\n",
       "      <td>1.383282</td>\n",
       "      <td>1.724718</td>\n",
       "      <td>0.712594</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>50</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Alabama</td>\n",
       "      <td>Autauga County</td>\n",
       "      <td>54571</td>\n",
       "      <td>54571</td>\n",
       "      <td>54660</td>\n",
       "      <td>...</td>\n",
       "      <td>7.242091</td>\n",
       "      <td>-2.915927</td>\n",
       "      <td>-3.012349</td>\n",
       "      <td>2.265971</td>\n",
       "      <td>-2.530799</td>\n",
       "      <td>7.606016</td>\n",
       "      <td>-2.626146</td>\n",
       "      <td>-2.722002</td>\n",
       "      <td>2.592270</td>\n",
       "      <td>-2.187333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>50</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>Alabama</td>\n",
       "      <td>Baldwin County</td>\n",
       "      <td>182265</td>\n",
       "      <td>182265</td>\n",
       "      <td>183193</td>\n",
       "      <td>...</td>\n",
       "      <td>14.832960</td>\n",
       "      <td>17.647293</td>\n",
       "      <td>21.845705</td>\n",
       "      <td>19.243287</td>\n",
       "      <td>17.197872</td>\n",
       "      <td>15.844176</td>\n",
       "      <td>18.559627</td>\n",
       "      <td>22.727626</td>\n",
       "      <td>20.317142</td>\n",
       "      <td>18.293499</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>50</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>Alabama</td>\n",
       "      <td>Barbour County</td>\n",
       "      <td>27457</td>\n",
       "      <td>27457</td>\n",
       "      <td>27341</td>\n",
       "      <td>...</td>\n",
       "      <td>-4.728132</td>\n",
       "      <td>-2.500690</td>\n",
       "      <td>-7.056824</td>\n",
       "      <td>-3.904217</td>\n",
       "      <td>-10.543299</td>\n",
       "      <td>-4.874741</td>\n",
       "      <td>-2.758113</td>\n",
       "      <td>-7.167664</td>\n",
       "      <td>-3.978583</td>\n",
       "      <td>-10.543299</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>50</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>Alabama</td>\n",
       "      <td>Bibb County</td>\n",
       "      <td>22915</td>\n",
       "      <td>22919</td>\n",
       "      <td>22861</td>\n",
       "      <td>...</td>\n",
       "      <td>-5.527043</td>\n",
       "      <td>-5.068871</td>\n",
       "      <td>-6.201001</td>\n",
       "      <td>-0.177537</td>\n",
       "      <td>0.177258</td>\n",
       "      <td>-5.088389</td>\n",
       "      <td>-4.363636</td>\n",
       "      <td>-5.403729</td>\n",
       "      <td>0.754533</td>\n",
       "      <td>1.107861</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 100 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   SUMLEV  REGION  DIVISION  STATE  COUNTY   STNAME         CTYNAME  \\\n",
       "0      40       3         6      1       0  Alabama         Alabama   \n",
       "1      50       3         6      1       1  Alabama  Autauga County   \n",
       "2      50       3         6      1       3  Alabama  Baldwin County   \n",
       "3      50       3         6      1       5  Alabama  Barbour County   \n",
       "4      50       3         6      1       7  Alabama     Bibb County   \n",
       "\n",
       "   CENSUS2010POP  ESTIMATESBASE2010  POPESTIMATE2010     ...       \\\n",
       "0        4779736            4780127          4785161     ...        \n",
       "1          54571              54571            54660     ...        \n",
       "2         182265             182265           183193     ...        \n",
       "3          27457              27457            27341     ...        \n",
       "4          22915              22919            22861     ...        \n",
       "\n",
       "   RDOMESTICMIG2011  RDOMESTICMIG2012  RDOMESTICMIG2013  RDOMESTICMIG2014  \\\n",
       "0          0.002295         -0.193196          0.381066          0.582002   \n",
       "1          7.242091         -2.915927         -3.012349          2.265971   \n",
       "2         14.832960         17.647293         21.845705         19.243287   \n",
       "3         -4.728132         -2.500690         -7.056824         -3.904217   \n",
       "4         -5.527043         -5.068871         -6.201001         -0.177537   \n",
       "\n",
       "   RDOMESTICMIG2015  RNETMIG2011  RNETMIG2012  RNETMIG2013  RNETMIG2014  \\\n",
       "0         -0.467369     1.030015     0.826644     1.383282     1.724718   \n",
       "1         -2.530799     7.606016    -2.626146    -2.722002     2.592270   \n",
       "2         17.197872    15.844176    18.559627    22.727626    20.317142   \n",
       "3        -10.543299    -4.874741    -2.758113    -7.167664    -3.978583   \n",
       "4          0.177258    -5.088389    -4.363636    -5.403729     0.754533   \n",
       "\n",
       "   RNETMIG2015  \n",
       "0     0.712594  \n",
       "1    -2.187333  \n",
       "2    18.293499  \n",
       "3   -10.543299  \n",
       "4     1.107861  \n",
       "\n",
       "[5 rows x 100 columns]"
      ]
     },
     "execution_count": 211,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "census_df = pd.read_csv('census.csv')\n",
    "census_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "umich_part_id": "005",
    "umich_partlist_id": "002"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Texas'"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def answer_five():\n",
    "    grouped = census_df[census_df['SUMLEV'] == 50]\n",
    "    grouped = grouped.groupby('STNAME').count()\n",
    "    grouped = grouped['SUMLEV'].argmax()\n",
    "    \n",
    "    return grouped\n",
    "\n",
    "answer_five()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Question 6\n",
    "**Only looking at the three most populous counties for each state**, what are the three most populous states (in order of highest population to lowest population)? Use `CENSUS2010POP`.\n",
    "\n",
    "*This function should return a list of string values.*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {
    "umich_part_id": "006",
    "umich_partlist_id": "002"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['California', 'Texas', 'Illinois']"
      ]
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def answer_six():\n",
    "    grouped = census_df[census_df['SUMLEV'] == 50]\n",
    "    grouped = grouped.sort(['STNAME','CENSUS2010POP'], ascending=False).groupby('STNAME').head(3)\n",
    "    result = (grouped.groupby('STNAME').sum().sort(['CENSUS2010POP'], ascending=False))[:3]\n",
    "    return list(result.index)\n",
    "\n",
    "answer_six()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Question 7\n",
    "Which county has had the largest absolute change in population within the period 2010-2015? (Hint: population values are stored in columns POPESTIMATE2010 through POPESTIMATE2015, you need to consider all six columns.)\n",
    "\n",
    "e.g. If County Population in the 5 year period is 100, 120, 80, 105, 100, 130, then its largest change in the period would be |130-80| = 50.\n",
    "\n",
    "*This function should return a single string value.*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "metadata": {
    "umich_part_id": "007",
    "umich_partlist_id": "002"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Harris County'"
      ]
     },
     "execution_count": 189,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def answer_seven():\n",
    "    grouped = census_df[census_df['SUMLEV'] == 50]\n",
    "    column_to_keep = ['CTYNAME', 'POPESTIMATE2010', 'POPESTIMATE2011', 'POPESTIMATE2012', 'POPESTIMATE2013', 'POPESTIMATE2014', 'POPESTIMATE2015']\n",
    "    grouped = grouped[column_to_keep]\n",
    "    df_grouped = grouped.set_index('CTYNAME')\n",
    "    df_grouped = df_grouped.iloc[:].std(axis=1)\n",
    "    return df_grouped.argmax()\n",
    "\n",
    "answer_seven()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Question 8\n",
    "In this datafile, the United States is broken up into four regions using the \"REGION\" column. \n",
    "\n",
    "Create a query that finds the counties that belong to regions 1 or 2, whose name starts with 'Washington', and whose POPESTIMATE2015 was greater than their POPESTIMATE 2014.\n",
    "\n",
    "*This function should return a 5x2 DataFrame with the columns = ['STNAME', 'CTYNAME'] and the same index ID as the census_df (sorted ascending by index).*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 232,
   "metadata": {
    "umich_part_id": "008",
    "umich_partlist_id": "002"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>STNAME</th>\n",
       "      <th>CTYNAME</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>896</th>\n",
       "      <td>Iowa</td>\n",
       "      <td>Washington County</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1419</th>\n",
       "      <td>Minnesota</td>\n",
       "      <td>Washington County</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2345</th>\n",
       "      <td>Pennsylvania</td>\n",
       "      <td>Washington County</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2355</th>\n",
       "      <td>Rhode Island</td>\n",
       "      <td>Washington County</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3163</th>\n",
       "      <td>Wisconsin</td>\n",
       "      <td>Washington County</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            STNAME            CTYNAME\n",
       "896           Iowa  Washington County\n",
       "1419     Minnesota  Washington County\n",
       "2345  Pennsylvania  Washington County\n",
       "2355  Rhode Island  Washington County\n",
       "3163     Wisconsin  Washington County"
      ]
     },
     "execution_count": 232,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def answer_eight():\n",
    "    df = census_df[census_df['SUMLEV'] == 50]\n",
    "    df = df[((df['REGION'] == 1) | (df['REGION'] == 2)) & df['CTYNAME'].str.startswith('Washington') & (df['POPESTIMATE2015'] > df['POPESTIMATE2014'])]\n",
    "    return df[['STNAME', 'CTYNAME']]\n",
    "\n",
    "answer_eight()"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "coursera": {
   "course_slug": "python-data-analysis",
   "graded_item_id": "tHmgx",
   "launcher_item_id": "Um6Bz",
   "part_id": "OQsnr"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "umich": {
   "id": "Assignment 2",
   "version": "1.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
